﻿using System.Collections.Generic;
using System.Linq;
using Fuse8.ContentManagementFramework.Domain;
using Fuse8.ContentManagementFramework.Domain.Commands;
using Fuse8.DomainFramework.Common.Locator;
using Fuse8.DomainFramework.DataAccess;
using Fuse8.DomainFramework.DataAccess.SqlDatabase;

namespace Fuse8.ContentManagementFramework.DataAccess.SqlAccess.Commands
{
    internal class CmsEntityMetaFieldLinkForTypeDataCommand : CmsEntityMetaFieldLinkForTypeCommand
    {
        public override System.Linq.IQueryable<Domain.CmsEntityMetaFieldLink> Execute()
        {
            IQueryable<Domain.CmsEntityMetaFieldLink> result = null;

            var factory = DependencyServiceLocator.Current.GetInstance<IUnitOfWorkFactory<CmsEntityMetaFieldLink>>();

            var currentUnitOfWork = factory.Get(false) as SqlUnitOfWork<CmsEntityMetaFieldLink>;

            if (currentUnitOfWork != null)
            {
                var mapper = DependencyServiceLocator.Current.GetInstance<ISqlEntityDataMapper<CmsEntityMetaFieldLink, int>>();

                result = SqlUtility<CmsEntityMetaFieldLink, int>.ExecuteReader(
                        currentUnitOfWork.Connection,
                        currentUnitOfWork.Transaction,
                        "cms_sp_EntityMetaFieldLinkSelectForType",
                        new
                        {
                            EntityTypeParamater = EntityType
                        },
                        mapper).AsQueryable();
            }

            return result ?? new List<CmsEntityMetaFieldLink>().AsQueryable();
        }
    }
}